package com.hooper.dp.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hooper.dp.entity.ShopType;
import com.hooper.dp.mapper.ShopTypeMapper;
import com.hooper.dp.service.IShopTypeService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.hooper.dp.constant.CommonConstant.COMMON_TTL;
import static com.hooper.dp.constant.ShopConstant.SHOP_TYPE_CACHE_KEY;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
@SuppressWarnings("unchecked")
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType>
        implements IShopTypeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<ShopType> shopTypeList() {
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String shopTypeJson = opsForValue.get(SHOP_TYPE_CACHE_KEY);
        if (StringUtils.isNotBlank(shopTypeJson)) {
            return JSONUtil.toList(shopTypeJson, ShopType.class);
        }
        LambdaQueryWrapper<ShopType> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(ShopType::getSort);
        List<ShopType> shopTypeList = this.list(wrapper);
        opsForValue.set(SHOP_TYPE_CACHE_KEY,
                JSONUtil.toJsonStr(shopTypeList),COMMON_TTL, TimeUnit.MINUTES);
        return shopTypeList;
    }

}
